<%@ page import="assignmentonepenguins.Customer; assignmentonepenguins.Bidding; assignmentonepenguins.Listing" %>
<!doctype html>
<html>
<head>
    <meta name="layout" content="main">
    <g:set var="entityName" value="${message(code: 'listing.label', default: 'Listing')}"/>
    <g:set var="bidEntityName" value="${message(code: 'bidding.label', default: 'Bid')}"/>
    <g:javascript library="jquery" plugin="jquery"/>
    <title><g:message code="default.show.label" args="[entityName]"/></title>
    <g:javascript>

        $(document).ready(function () {

            $('#bid').click(function () {
                $('#show-listing').toggle().fadeOut('slow')
                $('#create-bidding').toggle().fadeIn('slow')
            });
        });

        setInterval(refreshMe(),50000);
            function refreshMe()
            {
                ${remoteFunction(controller: 'listing', action: 'isListingExpired', params: '\'id=\' + listingInstance.id')}
        }
    </g:javascript>

</head>

<body>
<a href="#show-listing" class="skip" tabindex="-1"><g:message code="default.link.skip.label"
                                                              default="Skip to content&hellip;"/></a>

<div class="nav" role="navigation">
    <ul>
        <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
        <li><g:link class="create" action="create"><g:message code="default.new.label"
                                                              args="[entityName]"/></g:link></li>
        %{--<g:if test="${listingInstance.expired==false}">--}%
        <li><a href="javascript:void(0)" id="bid"><g:message code="New Bid"/></a></li>
        %{--</g:if>--}%

    </ul>
</div>

<div id="hello" style="display: none">

    New bid clicked
</div>

<div id="show-listing" class="content scaffold-show" role="main">
    <h1><g:message code="default.show.label" args="[entityName]"/></h1>
    <g:if test="${flash.message}">
        <div class="message" role="status">${flash.message}</div>
    </g:if>
    <ol class="property-list listing">

        <g:if test="${listingInstance?.name}">
            <li class="fieldcontain">
                <span id="name-label" class="property-label"><g:message code="listing.name.label"
                                                                        default="Name"/></span>

                <span class="property-value" aria-labelledby="name-label"><g:fieldValue bean="${listingInstance}"
                                                                                        field="name"/></span>

            </li>
        </g:if>

        <g:if test="${listingInstance?.description}">
            <li class="fieldcontain">
                <span id="description-label" class="property-label"><g:message code="listing.description.label"
                                                                               default="Description"/></span>

                <span class="property-value" aria-labelledby="description-label"><g:fieldValue bean="${listingInstance}"
                                                                                               field="description"/></span>

            </li>
        </g:if>

        <g:if test="${listingInstance?.winner}">
            <li class="fieldcontain">
                <span id="winner-label" class="property-label"><g:message code="listing.winner.label"
                                                                          default="Winner"/></span>

                <span class="property-value" aria-labelledby="winner-label"><g:link controller="customer" action="show"
                                                                                    id="${listingInstance?.winner?.id}">${listingInstance?.winner?.encodeAsHTML()}</g:link></span>

            </li>
        </g:if>

        <g:if test="${listingInstance?.biddings}">
            <li class="fieldcontain">
                <span id="currentBid-label" class="property-label"><g:message code="listing.biddings.label"
                                                                              default="Current Bid"/></span>

                <g:set var="maxBid" value="${new Bidding()}"/>
                <g:each in="${listingInstance.biddings}" var="b">
                    <g:if test="${b.amount > maxBid.amount}">
                        <g:set var="maxBid" value="${b}"/>
                    </g:if>
                </g:each>
                <span class="property-value" aria-labelledby="currentBid-label">$<g:fieldValue bean="${maxBid}"
                                                                                               field="amount"/></span>

            </li>
        </g:if>

    %{--<g:if test="${maxBid}">--}%
    %{--<li class="fieldcontain">--}%
    %{--<span id="minNewBid-label" class="property-label"><g:message code="listing.biddings.label"--}%
    %{--default="Min New Bid"/></span>--}%

    %{--<span class="property-value" aria-labelledby="currentBid-label">$<g:fieldValue bean="${maxBid}"--}%
    %{--field="amount"/></span>--}%

    %{--</li>--}%
    %{--</g:if>--}%

        <g:if test="${listingInstance?.startBid}">
            <li class="fieldcontain">
                <span id="startBid-label" class="property-label"><g:message code="listing.startBid.label"
                                                                            default="Start Bid"/></span>

                <span class="property-value" aria-labelledby="startBid-label">$<g:fieldValue bean="${listingInstance}"
                                                                                             field="startBid"/></span>

            </li>
        </g:if>
        <div class="fieldcontain ${hasErrors(bean: listingInstance, field: 'biddings', 'error')} ">
            <label for="biddings">
                <g:message code="listing.biddings.label" default="Recent Bids"/>

            </label>
            <g:if test="${listingInstance?.biddings}">
                <ul class="one-to-many">
                    <g:set var="counter" value="${0}"/>
                    <g:each in="${listingInstance?.biddings?.sort {x, y -> y.amount.compareTo(x.amount)}}" var="b">
                        <g:set var="counter" value="${counter + 1}"/>
                        <g:if test="${counter <= 10}">
                            <li><g:link controller="bidding" action="show"
                                        id="${b.id}">$${b?.getAmount()} - ${b?.getBidder()?.getEmail()}
                                (${b?.dateCreated.format("hh:mmaa MMM-dd")})</g:link></li>
                        </g:if>
                    </g:each>
                </ul>
            </g:if>

        </div>
        <g:if test="${listingInstance?.endDate}">
            <li class="fieldcontain">
                <span id="endDate-label" class="property-label"><g:message code="listing.endDate.label"
                                                                           default="End Date"/></span>

                <span class="property-value" aria-labelledby="endDate-label"><g:formatDate format="hh:mmaa MMM-dd-yyyy"
                                                                                           date="${listingInstance?.endDate}"/></span>

            </li>
        </g:if>

        <g:if test="${listingInstance?.seller}">
            <li class="fieldcontain">
                <span id="seller-label" class="property-label"><g:message code="listing.seller.label"
                                                                          default="Seller"/></span>

                <span class="property-value" aria-labelledby="seller-label"><g:link controller="customer" action="show"
                                                                                    id="${listingInstance?.seller?.id}">${listingInstance?.seller?.getEmail()[0..<listingInstance?.seller?.getEmail().indexOf('@')]}</g:link></span>

            </li>
        </g:if>

    </ol>
    <g:form>
        <fieldset class="buttons">
            <g:hiddenField name="id" value="${listingInstance?.id}"/>
            <g:link class="edit" action="edit" id="${listingInstance?.id}"><g:message code="default.button.edit.label"
                                                                                      default="Edit"/></g:link>
            <g:actionSubmit class="delete" action="delete"
                            value="${message(code: 'default.button.delete.label', default: 'Delete')}"
                            onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');"/>
        </fieldset>
    </g:form>
</div>

<div id="create-bidding" class="content scaffold-create" role="main" style="display: none">
    <h1><g:message code="Create New Bid amount:"/></h1>
%{--<data-field-id="New Bid">--}%

%{--Enter New Bid: $<g:field type="double" name="startBid" min="0.01" required=""--}%
%{--value="${fieldValue(bean: listingInstance, field: 'startBid')}"/>--}%

%{--<g:submitButton id="createButton" name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />--}%

    <g:formRemote name="myForm" update="show-listing" method="POST"
                  action="${createLink(controller: 'Listing', action: 'addBid')}"
                  url="[controller: 'Listing', action: 'addBid']"
                  after="location.reload()">
        Enter New Bid: $<input name="amount" type="double"/>
        <g:select id="bidder" style="display: none" name="bidderId" from="${assignmentonepenguins.Customer.list()}"
                  optionKey="id"
                  optionValue="email" required="" value="${biddingInstance?.bidder?.id}" class="many-to-one"/>
        <g:select id="listing" style="display: none" name="listingId" from="${assignmentonepenguins.Listing.list()}"
                  optionKey="id"
                  optionValue="name" required="" value="${biddingInstance?.listing?.id}" class="many-to-one"/>
        <g:submitButton id="createButton" name="create" class="save"
                        value="${message(code: 'default.button.create.label', default: 'Create')}"/>

    </g:formRemote>
</div>
</body>
</html>
